package com.hafu.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
    private static String URL = "jdbc:mysql://localhost:3306/db_jxcms1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private static String USERNAME = "root";
    private static String PASSWORD = "123456";
    private static Connection connection = null;

    public static Connection getConnection() {
        try {
            // 显式加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功");
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeConnection() {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println(DbUtil.getConnection());
    }
}
